package edu.hawaii.myisern.brown.action;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import edu.hawaii.myisern.brown.model.MyIsernModel;
import net.sourceforge.stripes.action.DontValidate;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.validation.SimpleError;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.validation.ValidationError;

/**
 * New Collaboration action bean.
 * 
 * @author rcox
 * 
 */
public class EditCollaborationActionBean extends MyIsernActionBean {

  @Validate(required = true)
  private String name;
  private String description;
  private List<String> collaboratingOrganizations = new ArrayList<String>();
  private List<String> collaborationTypes = new ArrayList<String>();
  private List<String> outcomeTypes = new ArrayList<String>();
  private List<BigInteger> years = new ArrayList<BigInteger>();

  private String newCollaboratingOrganization;
  private String newCollaborationType;
  private String newOutcomeType;
  private BigInteger newYear;

  /**
   * Get collaboration types.
   * 
   * @return the collaborationTypes
   */
  public List<String> getCollaborationTypes() {
    return collaborationTypes;
  }

  /**
   * Set collaboration types.
   * 
   * @param collaborationTypes the collaborationTypes to set
   */
  public void setCollaborationTypes(List<String> collaborationTypes) {
    this.collaborationTypes = collaborationTypes;
  }

  /**
   * @return the collaborationTypesStr
   */
  public String getNewCollaborationType() {
    return newCollaborationType;
  }

  /**
   * @param newCollaborationType the collaborationTypesStr to set
   */
  public void setNewCollaborationType(String newCollaborationType) {
    this.newCollaborationType = newCollaborationType;
  }

  /**
   * Get Collaborating Org string.
   * 
   * @return the collaboratingOrganizationsStr
   */
  public String getNewCollaboratingOrganization() {
    return newCollaboratingOrganization;
  }

  /**
   * Set collaborating org string.
   * 
   * @param newCollaboratingOrganization the collaboratingOrganizationsStr to set
   */
  public void setNewCollaboratingOrganization(String newCollaboratingOrganization) {
    this.newCollaboratingOrganization = newCollaboratingOrganization;
  }

  /**
   * Get collaborating type string.
   * 
   * @return the collaboratingTypesStr
   */
  public String getNewCollaboratingType() {
    return newCollaborationType;
  }

  /**
   * Set collaborating type string.
   * 
   * @param newCollaboratingType the collaboratingTypesStr to set
   */
  public void setNewCollaboratingType(String newCollaboratingType) {
    this.newCollaborationType = newCollaboratingType;
  }

  /**
   * Get outcome types.
   * 
   * @return the outcomeTypesStr
   */
  public String getNewOutcomeType() {
    return newOutcomeType;
  }

  /**
   * Set outcome types.
   * 
   * @param newOutcomeType the outcomeTypesStr to set
   */
  public void setNewOutcomeType(String newOutcomeType) {
    this.newOutcomeType = newOutcomeType;
  }

  /**
   * Get year str.
   * 
   * @return the yearsStr
   */
  public synchronized BigInteger getNewYear() {
    return newYear;
  }

  /**
   * Set year str.
   * 
   * @param newYear the yearsStr to set
   */
  public void setNewYear(BigInteger newYear) {
    this.newYear = newYear;
  }

  /**
   * Returns the name.
   * 
   * @return the name
   */
  public String getName() {
    return name;
  }

  /**
   * Sets the name.
   * 
   * @param name the name to set
   */
  public void setName(String name) {
    this.name = name;
  }

  /**
   * Gets the description.
   * 
   * @return the description
   */
  public String getDescription() {
    return description;
  }

  /**
   * Sets the description.
   * 
   * @param description the description to set
   */
  public void setDescription(String description) {
    this.description = description;
  }

  /**
   * Gets the collaborating organizations.
   * 
   * @return the collaboratingOrganizations
   */
  public List<String> getCollaboratingOrganizations() {
    return collaboratingOrganizations;
  }

  /**
   * Sets the collaborating organizations.
   * 
   * @param collaboratingOrganizations the collaboratingOrganizations to set
   */
  public void setCollaboratingOrganizations(List<String> collaboratingOrganizations) {
    this.collaboratingOrganizations = collaboratingOrganizations;
  }

  /**
   * Gets the collaborating types.
   * 
   * @return the collaboratingTypes
   */
  public List<String> getCollaboratingTypes() {
    return collaborationTypes;
  }

  /**
   * Sets the collaborating types.
   * 
   * @param collaboratingTypes the collaboratingTypes to set
   */
  public void setCollaboratingTypes(List<String> collaboratingTypes) {
    this.collaborationTypes = collaboratingTypes;
  }

  /**
   * Gets the outcome types.
   * 
   * @return the outcomeTypes
   */
  public List<String> getOutcomeTypes() {
    return outcomeTypes;
  }

  /**
   * Sets the outcome types.
   * 
   * @param outcomeTypes the outcomeTypes to set
   */
  public void setOutcomeTypes(List<String> outcomeTypes) {
    this.outcomeTypes = outcomeTypes;
  }

  /**
   * Gets the years.
   * 
   * @return the years
   */
  public List<BigInteger> getYears() {
    return years;
  }

  /**
   * Sets the years.
   * 
   * @param years the years to set
   */
  public void setYears(List<BigInteger> years) {
    this.years = years;
  }

  /**
   * Loads a record on to the form ready for editing.
   * 
   * @return resolution of next page.
   */
  @DontValidate
  public Resolution preEdit() {
    Map<String, String> map = model.getOrganizations();
    List<String> list = new ArrayList<String>();
    list.addAll(map.values());
    getContext().getRequest().getSession().setAttribute("organizations", list);
    if (this.name != null) {
      this.model.fillCollaboration(this, MyIsernModel.makeId(this.name));
    }
    return new RedirectResolution("/editCollaboration.jsp").flash(this);
  }

  /**
   * Saves the record.
   * 
   * @return resolution to main menu.
   */
  public Resolution save() {
    include(this.newYear, this.years);
    include(this.newOutcomeType, this.outcomeTypes);
    include(this.newCollaborationType, this.collaborationTypes);
    include(this.newCollaboratingOrganization, this.collaboratingOrganizations);

    this.newCollaboratingOrganization = "";
    this.newCollaborationType = "";
    this.newOutcomeType = "";
    this.newYear = null;

    try {
      this.model.storeCollaboration(this);
      this.model.save();
    }
    catch (Exception e) {
      ValidationError error = new SimpleError("Error saving record, please check data "
          + "and re-submit. " + e.getMessage());
      getContext().getValidationErrors().add("name", error);
      return getContext().getSourcePageResolution();

    }

    return new RedirectResolution("/editCollaboration.jsp").flash(this);
  }

  /**
   * Returns to main menu and does not save data.
   * 
   * @return resolution to main menu.
   */
  @DontValidate
  public Resolution cancel() {
    return new RedirectResolution("/mainMenu.jsp");
  }

}
